<?php

defined('BASEPATH') or die('forbidden');

class Api_Controller Extends Controller {

    private $data = array();

    function _init() {
        header("Content-Type: application/json; charset=utf-8");
        $result = $this->input->deep_stripslashes($_POST);
        if ($result['sig'] == get_sig($result)) {
            $this->data = isset($result['data']) ? get_json_array($result['data']) : array();
            $method = str_replace("/", "_", $result['url']);
            if (method_exists($this, "_" . $method)) {
                $this->{"_{$method}"}();
            }
        } else {
            json_output(0, '签名错误');
        }
    }

    //读取所有分区信息
    function _area_info() {
        $result = array();
        $this->db->query("SELECT * FROM {pre}areasample WHERE father_id = id ORDER BY weigh DESC");
        $result[] = array('id' => '', 'name' => $this->config['area_name'], 'url' => base_url(1));
        while ($row = $this->db->get_array()) {
            $row['url'] = str_replace("/api/", "", $row['url']) . site_url();
            $result[] = $row;
        }
        json_output(1, $result);
    }

    //分区充值同步
    function _order_recharge_add() {
        $this->db->insert("{pre}order_recharge", $this->data);
        json_output(1);
    }

    function _account_account_info() {
        $account = $this->db->one("SELECT * FROM {pre}account WHERE pf='{$this->data['pf']}' AND openid='{$this->data['openid']}'");
        if ($account) {
            get_datetime(array(&$account['jointime'], &$account['logintime']), 1);
            json_output(1, $account);
        } else {
            json_output(0, '账户未找到');
        }
    }

}